package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.CheckinoutMorningBean;
import com.attendance.www.hr.dao.mapper.CheckinoutMorningMapper;
import com.attendance.www.hr.dao.mapper.EmployeeMapper;
import com.attendance.www.hr.entitys.CheckinoutMorning;
import com.attendance.www.hr.entitys.CheckinoutMorningExample;
import com.attendance.www.hr.entitys.Employee;
import com.attendance.www.hr.service.ICheckinoutMorningService;
import com.attendance.www.hr.utils.TimeUtils;

@Service
public class CheckinoutMorningServiceImpl implements ICheckinoutMorningService {

	@Resource
	private CheckinoutMorningMapper checkinoutMorningDao;
	@Resource
	private EmployeeMapper employeeDao;
	
	/**
	 * 查询早上打卡记录
	 */
	@Override
	public List<CheckinoutMorningBean> getCheckinoutMorningList(CheckinoutMorningBean checkinoutMorningBean) {
		List<CheckinoutMorningBean> cmbList = new ArrayList<>();
		if (this.getMorningTime(checkinoutMorningBean)==1) {
			//查人员No
			Employee emp=employeeDao.selectByPrimaryKey(checkinoutMorningBean.getEmployeeId());
			
			CheckinoutMorningExample cmExample = new CheckinoutMorningExample();
			cmExample.createCriteria().andEmployeeNoEqualTo(emp.getEmployeeNo())
			.andAttendanceDateEqualTo(DateUtils.addDays(checkinoutMorningBean.getAttendanceDate(), 1));//查传日期第二天早上的打卡
			List<CheckinoutMorning> cmList = checkinoutMorningDao.selectByExample(cmExample);
			for (CheckinoutMorning checkinoutMorning : cmList) {
				CheckinoutMorningBean cmb = new CheckinoutMorningBean();
				cmb.setAttendanceDate(checkinoutMorning.getAttendanceDate());
				cmb.setCheckinoutMorningId(checkinoutMorning.getCheckinoutMorningId());
				cmb.setCreateTime(checkinoutMorning.getCreateTime());
				cmb.setEmployeeNo(checkinoutMorning.getEmployeeNo());
				cmb.setMorningTime(checkinoutMorning.getMorningTime());
				cmb.setUpdateTime(checkinoutMorning.getUpdateTime());
				cmbList.add(cmb);
			}
		}
		return cmbList;
	}
	/**
	 * 查7：30前有没有打卡时间
	 */
	@Override
	public Integer getMorningTime(CheckinoutMorningBean checkinoutMorningBean) {
		// TODO Auto-generated method stub
		List<CheckinoutMorningBean> cmbList = new ArrayList<>();
		//查人员No
		Employee emp=employeeDao.selectByPrimaryKey(checkinoutMorningBean.getEmployeeId());
		
		CheckinoutMorningExample cmExample = new CheckinoutMorningExample();
		cmExample.createCriteria().andEmployeeNoEqualTo(emp.getEmployeeNo())
		.andAttendanceDateEqualTo(DateUtils.addDays(checkinoutMorningBean.getAttendanceDate(), 1));//查传日期第二天早上的打卡
		List<CheckinoutMorning> cmList = checkinoutMorningDao.selectByExample(cmExample);
		for (CheckinoutMorning checkinoutMorning : cmList) {
			//比较时间7：30
			String morningTime = checkinoutMorning.getMorningTime();
			if (TimeUtils.compareTime(morningTime, "07:30")<= 0) {
				CheckinoutMorningBean cmb = new CheckinoutMorningBean();
				cmb.setAttendanceDate(checkinoutMorning.getAttendanceDate());
				cmb.setCheckinoutMorningId(checkinoutMorning.getCheckinoutMorningId());
				cmb.setCreateTime(checkinoutMorning.getCreateTime());
				cmb.setEmployeeNo(checkinoutMorning.getEmployeeNo());
				cmb.setMorningTime(morningTime);
				cmb.setUpdateTime(checkinoutMorning.getUpdateTime());
				cmbList.add(cmb);
			}
		}
		if (cmbList.size()>0) {
			return 1;
		}
		return 0;
	}
	
	/**
	 * 删除三个月前的数据，自动执行
	 */
	@Override
	public void deleteDatas() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -3);
        Date compareDate=calendar.getTime();
		CheckinoutMorningExample cmExample = new CheckinoutMorningExample();
		cmExample.createCriteria().andAttendanceDateLessThanOrEqualTo(compareDate);
		checkinoutMorningDao.deleteByExample(cmExample);
	}

}
